<?php
/**
 * 项目基类
 * 
 * @author:  will.lee <im.will.lee@gmail.com>
 * @version: 1.0
 */
class AppAction extends Action
{
    protected $data = null;
    
    /**
     * 项目初始化方法
     * @access protected
     */
    protected function _initialize()
    {
        if(IS_POST || IS_AJAX)
        {
            foreach($_POST as $key => &$val)
            {
                if($val == '')
                {
                    $val = null;
                }
            }
            $this->data  = $_POST;
        }
    }
    
    /**
     * 渲染模板前置方法
     * @access protected
     */
    protected function _before_display()
    {
        $this->assign('_data',$this->data);
    }
    
    /**
     * 生成验证码
     * @access public
     */
    public function captcha()
    {
        Vendor('Kcaptcha.kcaptcha');
        $kcaptcha    = new KCAPTCHA();
        $session_val = $kcaptcha->getKeyString();
        session(C('ADMIN_VERIFY_KEY'),md5($session_val));
    }
    
    /**
     * 成功提示
     * @access protected
     */
     protected function success($options,$ajax = false,$attr = false)
     {
         $options['status'] = 1;
         $this->alert($options,$ajax);
     }
    
    /**
     * 错误提示
     * @access protected
     */
     protected function error($options,$ajax = false,$attr = false)
     {
         $options['status'] = 0;
         $this->alert($options,$ajax);
     }
    
    /**
     * 信息提示
     * @access private
     */
    private function alert($options,$ajax = false) {
        if(true === $ajax || IS_AJAX) 
        {
            // AJAX提交
            $this->ajaxReturn($options);
        }
        //保证输出不受静态缓存影响
        C('HTML_CACHE_ON',false);
        // 设置默认3秒跳转
        if(!isset($options['wait_time']))
        {
            $options['wait_time'] = 3000;
        }
        else
        {
            $options['wait_time'] = $options['wait_time']*1000;
        }
        // 绑定页面变量
        $this->assign('options',$options);
        if($options['status'] == 1) 
        { 
            //发送成功信息
            $this->display(C('TMPL_ACTION_SUCCESS'));
        }
        else
        {
            //发送错误信息
            $this->display(C('TMPL_ACTION_ERROR'));
        }
        // 中止执行  避免出错后继续执行
        exit ;
    }
    
    /**
     * 重构核心display方法
     * 增加_before_display()操作
     * 模板显示 调用内置的模板引擎显示方法，
     * @access protected
     * @param string $templateFile 指定要调用的模板文件
     * 默认为空 由系统自动定位模板文件
     * @param string $charset 输出编码
     * @param string $contentType 输出类型
     * @param string $content 输出内容
     * @param string $prefix 模板缓存前缀
     * @return void
     */
    protected function display($templateFile='',$charset='',$contentType='',$content='',$prefix='')
    {
        if(method_exists($this,'_before_display'))
        {
            $this->_before_display();
        }
        parent::display($templateFile,$charset,$contentType,$content,$prefix);
    }
    
    /**
     * 上传文件
     * 
     * @access protected
     * @param  string  $type 类型    1.图片  2.附件
     * @param  string  $path 保存路径
     * @return string  上传后路径
     */
    protected function upload($type = 1,$path)
    {
        $return      = array();
        // 允许类型
        $allow_exts  = null;
        // 默认无限制
        $max_size    = -1;
        // 图片
        if($type == 1)
        {
            $allow_exts = C('Config.image_allow_exts');
            $max_size   = C('Config.image_max_size')*1024;
        }
        else
        {
            $allow_exts = C('Config.attachment_allow_exts');
            $max_size   = C('Config.attachment_max_size')*1024;
        }
        import('ORG.Net.UploadFile');
        $upload = new UploadFile();
        $upload->maxSize    = $max_size ;
        $upload->allowExts  = $allow_exts;
        $upload->savePath   = $path;
        if(!$upload->upload())
        {
            $return['state']   = false;
            $return['message'] = $upload->getErrorMsg();
        }
        else
        {
            $return['state']   = true;
            $return['info']    = $upload->getUploadFileInfo();
        }
        return $return;
    }
}